<template>
  <el-card class="box-card card-main" shadow="never">
    <div slot="header">
      <el-form
        :inline="true"
        :model="serchRole"
        class="demo-form-inline"
        label-width="90px"
        label-position="right"
        size="mini"
      >
        <el-form-item label="用户名：">
          <el-input v-model="serchRole.name" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="用户描述：">
          <el-input v-model="serchRole.description" placeholder="请输入用户描述" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchList">查询</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-row>
      <el-button type="primary" size="mini" @click="handleAddOrEdit()">添加角色</el-button>
    </el-row>
    <el-table
      v-loading="listLoading"
      :data="roleList"
      border
      stripe
      size="small"
      style="width: auto"
    >
      <el-table-column type="selection" align="left" width="35" />
      <el-table-column prop="name" align="left" label="角色名称" />
      <el-table-column prop="description" align="left" label="角色描述" />
      <el-table-column width="150" align="left" label="创建时间">
        <template slot-scope="scope">{{ scope.row.createTime }}</template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            style="padding: 3px 4px 3px 4px;margin: 1px"
            size="mini"
            @click="handleAddOrEdit(scope.row)"
          >查看/编辑权限</el-button>
          <el-button
            v-if="scope.row.username !== 'admin'"
            type="danger"
            style="padding: 3px 4px 3px 4px;margin: 1px"
            size="mini"
            @click="deleteRow(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="pageInfo.pageNum"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="pageInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageInfo.pageCount"
      @size-change="changePageSize"
      @current-change="changePageNum"
    />
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrEditFormVisible" ref="addOrUpdate" @refreshDataList="getUserPageList" />
  </el-card>
</template>

<script>
import { requestAllRolesByPage, requestDelRoleById } from '@/api/role'
import AddOrUpdate from './role-add-or-update'
export default {
  components: {
    AddOrUpdate
  },
  data() {
    return {
      listLoading: false,
      serchRole: { name: '', description: '' },
      pageInfo: { pageNum: 1, pageSize: 10, pageCount: 0 },
      roleList: [],
      addOrEditFormVisible: false // 新增或者删除弹出框默认值
    }
  },
  computed: {
    serchInfo: function() {
      const pageInfo = this.pageInfo
      pageInfo.serchRole = this.serchRole
      return pageInfo
    }
  },
  mounted() {
    this.getUserPageList()
  },
  methods: {
    handleAddOrEdit(row) {
      this.addOrEditFormVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(row)
      })
    },
    deleteRow(row) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        requestDelRoleById(row.id).then(data => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getUserPageList()
        })
      }).catch(() => {
        this.$message({
          type: 'success',
          message: '已取消删除'
        })
      })
    },
    resetSearch() {
      // 重置搜索条件方法
      this.serchRole = {}
    },
    searchList() {
      this.getUserPageList()
    },
    changePageSize(data) {
      // console.log('分页插件修改每页条数事件方法' + data)
      this.pageInfo.pageSize = data
      this.requestUsersInfo()
    },
    changePageNum(data) {
      // console.log('修改当前页码' + data)
      this.pageInfo.pageNum = data
      this.requestUsersInfo()
    },
    // 获取分页用户列表
    getUserPageList() {
      this.listLoading = true
      requestAllRolesByPage(this.serchInfo).then((result) => {
        this.roleList = result.list || {}
        this.pageInfo.pageCount = result.total
        this.listLoading = false
      })
    }
  }
}
</script>

<style scoped>
</style>
